import { createRouter, createWebHistory } from 'vue-router'
import pageList from './pageList'
// 引入所有页面

  import nFBhLfqS from "../views/nFBhLfqS.vue"

  import knowledgePointManagementList from "../views/knowledgePointManagementList.vue"


const routes = [
  
  
    
{
  path: "/edit",
  name: "nFBhLfqS",
  component: nFBhLfqS,
  meta: {
    title: "知识点管理-编辑页",
  },
  children: [
    
  ]
},

  
    
{
  path: "/list",
  name: "knowledgePointManagementList",
  component: knowledgePointManagementList,
  meta: {
    title: "知识点管理列表",
  },
  children: [
    
  ]
},

  
  
  
     
  
     
      {
        path: "/",
        redirect: "/list",
      },
    
  
];
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})



router.beforeEach((to,from,next)=>{
 
   // 截取前五位 /page 剩下的匹配id
  const str = to.path.substring(5, to.path.length);
  /* 如果是uuid的话 跳转页面过来的
   * 如果是path的话说明是转换过来或者手输入的
   * 
  */
  const pageInfo = pageList.find(
    (item) => item.uuid === str || item.layoutPath === to.path
  ) || {};
 
  const loginObj = pageList.find((item) => item.loginRegisterType) || {};
  // 如果是登录页的话直接跳转
  if (
     [loginObj.layoutPath].includes(to.path) 
  ) {
    next();
    return;
  } else if (
    pageInfo.loginRequired &&
    !sessionStorage.getItem("token") &&
    loginObj
  ) {
    // 如果跳转页面不是免登录并且没有token 跳转登录页
    next({ path: loginObj.layoutPath });
    return;
  } else if(pageInfo.uuid === str){
    // 如果是跳转页面过来的跳转到对应path去
    next({...to, path: pageInfo.layoutPath });
    return 
  }
  next();
})
export default router;
